import requests
import re

# # url = "https://quotes.toscrape.com/tag/love/"
# # response = requests.get(url)
# # html = response.text
# # types = re.findall(r'<a class="tag" style="font-size: (.*?)px" href="/tag/(.*?)/">(.*?)</a>',html,re.S)
# # for type in types:
# #     now_type=type[1]
# #     now_url=f"https://quotes.toscrape.com/tag/{now_type}/"
# #     res = requests.get(now_url)
# #     now_html = res.text
# #     texts = re.findall(r'<span class="text" itemprop="text">“(.*?)”</span>', now_html, re.S)
# #     authors = re.findall(r' <small class="author" itemprop="author">(.*?)</small>', now_html, re.S)
# #     tags = re.findall(r'<meta class="keywords" itemprop="keywords" content="(.*?)" /', now_html, re.S)
# #     types = re.findall(r'<a class="tag" style="font-size: (.*?)px" href="/tag/(.*?)/">(.*?)</a>', now_html, re.S)
# #     for item in range(len(texts)):
# #         print(f"正在爬取{now_type}里面的第{item+1}条数据")
# #         print("文章： "+texts[item])
# #         print("作者： "+authors[item])
# #         print("标签： "+tags[item])
#
# #登录
# #方式一
# # headers = {
# #     "cookie": "session=eyJjc3JmX3Rva2VuIjoicEdUZEFMd2lidU1tdmpZa1ZQWGVGb0JXSEtnYW5zSVNmSkV5Q3FaaHJ0eGxEUlVRT05jeiIsInVzZXJuYW1lIjoiMTIzNDUifQ.Z-z1dQ.MloJDJS0dbpCXk9PoIs_VpZhPwM.Z-zz9w.LrxmSGN6Mw2F3bG83W8sl3WbzEE"
# # }
# # res = requests.get("https://quotes.toscrape.com/",headers = headers)
# # state = re.search(r'<div class="col-md-4">.*?<a href=".*?">(.*?)</a>',res.text,re.S)
# # print(state.group(1))
#
# #方式二 session
# # session = requests.Session()
# # print(session)
# #
# # session.post("https://quotes.toscrape.com/login",data = {
# #     "username": "123456",
# #     "password": "123456"
# # },allow_redirects=False)
# # print(session.cookies)
# #
# # res = session.get("https://quotes.toscrape.com")
# # state = re.search(r'<div class="col-md-4">.*?<a href=".*?">(.*?)</a>',res.text,re.S)
# # print(state.group(1))
#
# # 1. 想登录页面发起get请求  获取csrf_token 以及cookie
# login_get_res = requests.get("https://quotes.toscrape.com/login")
# print("发起get请求获取cookie", login_get_res.cookies)
#
# csrf_token_text = re.search(r'<input type="hidden" name="csrf_token" value="(.*?)"/>', login_get_res.text, re.S)
# if csrf_token_text:
# 	csrf_token = csrf_token_text.group(1)
# 	# 2. 发post请求 携带cookie与csrf_token
# 	login_post_res = requests.post("https://quotes.toscrape.com/login", data={
# 		"csrf_token": csrf_token,
# 		"username": "admin",
# 		"password": "123456"
# 	}, cookies=login_get_res.cookies, allow_redirects=False)
# 	print("发起post请求获取cookie", login_post_res.cookies)
# 	res = requests.get("https://quotes.toscrape.com/", cookies=login_post_res.cookies)
# 	state = re.search(r'<div class="col-md-4">.*?<a href=".*?">(.*?)</a>', res.text, re.S)
# 	print(state.group(1))


result = []
for page in range(1, 11):
    print(f"正在爬取第{page}页.............")
    url = f"https://quotes.toscrape.com/page/{page}/"
    res = requests.get(url)
    items = re.findall(r'<div class="quote"(.*?)</div>', res.text, re.S)
    for item in items:
        print(item)
        content = re.search(r'<span class="text" itemprop="text">“(.*?)”</span>', item, re.S)
        # print(content.group(1))

        author = re.search(r'itemprop="author">(.*?)</small>', item, re.S)
        # print(author.group(1))

        tags = re.findall(r'<a class="tag" href=".*?">(.*?)</a>', item, re.S)
        # print(tags)

        result.append({
            "content": content.group(1),
            "author": author.group(1),
            "tags": tags
        })
    print(result)
